package cetcbigdata.da.personal_corporate_matters_guide.hubei;

import cetcbigdata.da.base.Base;
import com.alibaba.datax.common.plugin.RecordSender;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class newSpiderHB extends Base{

    @Override
    public void getData(RecordSender recordSender, List<Object> fieldsList) {

    }

    @Override
    public JSONObject getDataDemo() {
        try {
            return this.parseListPage();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
    private JSONObject parseListPage(){
        try {

//              1.1对参数进行特殊化处理
                int currentPage = 1;

//               1，2开始访问列表页
                String listUrl = "http://zwfw.hubei.gov.cn/web/bszn/item_project";
                JSONObject parmas = new JSONObject();
                parmas.put("serviceGr", "1");
//              System.out.println(cs);
                parmas.put("regionCode", "420000000000");
                parmas.put("areaGrade", "2");
                parmas.put("currentPage", currentPage);
                parmas.put("pageSize", "10");
                String listResponse = postFormReq(listUrl, new JSONObject(), parmas);
                JSONObject responseObj = JSONObject.parseObject(listResponse);
//                获取总页数为翻页做准备
                int Page = responseObj.getJSONObject("data").getInteger("pages");

                for (;currentPage <= Page;) {
                    System.out.println("currentPage:"+currentPage);
                    System.out.println("page:"+Page);
//                System.out.println("---------------打印jsonobject对象--------------------------------------");
//                System.out.println(listResponse);
                    if (responseObj.getString("status").equals("200")) {
//                   System.out.println("--------------------打印jsonobject对象中records中的信息，获取一个项目中有多少个结点---------------------------------");
                        JSONArray list = responseObj.getJSONObject("data").getJSONArray("records");
//                  System.out.println("这是records"+list);

                        for (Object o : list) {
//                        2.1 想要获取打开请求头的网页，就要先获取到post请求url的头部信息 先获取 TASKCODE
                            String item = ((JSONObject) o).getString("TASKCODE");
                            System.out.println(item);
//                      2.2 想要获取打开请求头的网页，就要 先获取到post请求url的头部信息      actionCode


                            String listUrlnext = "http://zwfw.hubei.gov.cn/web/tysl/ykypt/standard/transact/project/getActionByTaskCode";
                            JSONObject parmasnext = new JSONObject();
                            parmasnext.put("taskCode", item);
                            String listResponsenext = postFormReq(listUrlnext, new JSONObject(), parmasnext);
//                        System.out.println(listResponsenext);

//                        2.2.3###出现了第二种情况 即是 actionCodelist为空的情况
                            JSONObject responseObjnext = JSONObject.parseObject(listResponsenext);
                            JSONArray actionCodelist = JSONArray.parseArray(responseObjnext.getString("data"));
                            if (!isNotBlankOrNull(actionCodelist)) {
                                System.out.println("错误查找");
//                                        直接获取当前网站的  TRANSACTCODE
                                String specialListUrl = "http://zwfw.hubei.gov.cn/web/bszn/item_project_transaction";
                                JSONObject SpecialParmasnext = new JSONObject();
//                            System.out.println(item);
                                SpecialParmasnext.put("taskcode", item);
                                SpecialParmasnext.put("currentPage", currentPage);
                                SpecialParmasnext.put("pageSize", "5");
                                String SpeciallistResponsenext = postFormReq(specialListUrl, new JSONObject(), SpecialParmasnext);
//                            System.out.println(SpeciallistResponsenext);
                                JSONObject SpecialresponseObj = JSONObject.parseObject(SpeciallistResponsenext);
                                JSONArray list2 = SpecialresponseObj.getJSONObject("data").getJSONArray("records");
//                            System.out.println("这是list2 :" +SpeciallistResponsenext );
                                for (int k = 0; k < list2.size(); k++) {
                                    JSONObject jsonObject2 = list2.getJSONObject(k);
                                    String TRANSACTCODE = jsonObject2.getString("TRANSACTCODE");
//                                System.out.println("这是特殊网页的TRANSACTCODE"+TRANSACTCODE);
//                                开始获取详情页的信息
                                    String specialdetailurl = "http://zwfw.hubei.gov.cn/web/bszn/ykypt/standard/transact/project/getTransactInfoByTransactCode.jspc";
                                    JSONObject specialdetailparmas1 = new JSONObject();
                                    specialdetailparmas1.put("transactCode", TRANSACTCODE);
                                    specialdetailparmas1.put("type", "legalbasis");
                                    String specialdetailResponse = postFormReq(specialdetailurl, new JSONObject(), specialdetailparmas1);
//                              System.out.println(detailResponse);
                                    List<JSONObject> content = this.parseDetailPage(specialdetailResponse);
                                }

                            } else {

                                String actionCode = "";
                                for (int i = 0; i < actionCodelist.size(); i++) {
                                    JSONObject jsonObject1 = actionCodelist.getJSONObject(i);
                                    actionCode = jsonObject1.getString("actionCode");
//                           访问下一个子网页 获取 TransactCode
                                    String listurl2 = "http://zwfw.hubei.gov.cn/web/tysl/ykypt/standard/item/transact/selectPageList";
                                    JSONObject parmasnext2 = new JSONObject();
                                    parmasnext.put("taskCode", item);
                                    parmasnext.put("actionCode", actionCode);
                                    String listResponsenext2 = postFormReq(listurl2, new JSONObject(), parmasnext);
//                                System.out.println("打印selcetPageList网页");
//                                System.out.println(listResponsenext2);
                                    JSONObject responseObjnext2 = JSONObject.parseObject(listResponsenext2);
                                    JSONObject TransactCodefirst = JSONObject.parseObject(responseObjnext2.getString("data"));
//                                System.out.println(TransactCodefirst);
                                    JSONArray TransactCodefirstlist = TransactCodefirst.getJSONArray("data");
//                                System.out.println(TransactCodefirstlist);
                                    String TransactCode;
                                    for (int j = 0; j < TransactCodefirstlist.size(); j++) {
                                        JSONObject jsonObject2 = TransactCodefirstlist.getJSONObject(j);
                                        TransactCode = jsonObject2.getString("TransactCode");
//                              System.out.println(TransactCode);
//                            3.1现在开始解析详情页
                                        String detailUrl = "http://zwfw.hubei.gov.cn/web/bszn/ykypt/standard/transact/project/getTransactInfoByTransactCode.jspc";
                                        JSONObject detailparmas1 = new JSONObject();
                                        detailparmas1.put("transactCode", TransactCode);
                                        detailparmas1.put("type", "legalbasis");
                                        String detailResponse = postFormReq(detailUrl, new JSONObject(), detailparmas1);
//                                  System.out.println(detailResponse);
                                        List<JSONObject> content = this.parseDetailPage(detailResponse);
//                                    System.out.println(content);
                                    }
                                }

                            }

                        }
                    }
                    ++currentPage;
                    JSONObject parmasnext = new JSONObject();
//              System.out.println(cs);
                  ;
                    parmasnext.put("regionCode", "420000000000");
                    parmasnext.put("areaGrade", "2");
                    parmasnext.put("currentPage", currentPage);
                    parmasnext.put("pageSize", "10");
                    listResponse = postFormReq(listUrl, new JSONObject(), parmasnext);
                    responseObj= JSONObject.parseObject(listResponse);
                }

                //
                return null;


        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    //解析详情页
    private List<JSONObject> parseDetailPage(String json){

        JSONObject detailobj = JSONObject.parseObject(json);
        JSONArray detaillist =JSONArray.parseArray(detailobj.getString("data"));
//        System.out.println(detaillist);
        List<JSONObject> jsonObjectList = new ArrayList<>();
        for(Object o:detaillist)
        {   JSONObject res = new JSONObject();
            Iterator<String> iterator = ((JSONObject)o).keySet().iterator();
            while (iterator.hasNext()) {
                String key = iterator.next();
//              System.out.println(key);
                res.put("号令",((JSONObject)o).getString("NUM"));
                res.put("法规原文链接",((JSONObject)o).getString("Url"));
                res.put("生效日期",((JSONObject)o).getString("EffectDay"));
                res.put("发布日期",((JSONObject)o).getString("EffectDay"));
                res.put("标题",((JSONObject)o).getString("NAME"));
                res.put("颁布机构",((JSONObject)o).getString("IssueDept"));
                res.put("条款内容",((JSONObject)o).getString("LawItems"));

            }
            jsonObjectList.add(res);
        }
//          System.out.println(jsonObjectList);
        return jsonObjectList;
    }



    public static void main (String[] args)
    {
       newSpiderHB newSpiderHB = new newSpiderHB();
        System.out.println(newSpiderHB.getDataDemo());


    }








}
